package org.tity.maven;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.tity.commons.lang.StringUtil;

import jodd.io.FileUtil;

public class MavenBuilder {

	 
	
	private static final String MAVEN_HOME="D:\\devtoosl\\maven\\apache-maven-3.5.3\\";
	private static final String SETTINGS_FILE="D:\\devtoosl\\maven\\apache-maven-3.5.3\\conf\\settings-sat-nexus.xml";
	private static final File WORK_DIR=new File("D:\\devtoosl\\git\\local\\tity-commons\\tity-parent");
	
	public static final String[] POMS= {
			
			"D:\\devtoosl\\git\\local\\tity-commons\\tity-parent\\pom.xml",
			"D:\\devtoosl\\git\\local\\tity-commons\\tity-parent\\base-pom.xml",
			"D:\\devtoosl\\git\\local\\tity-commons\\tity-commons\\pom.xml",
			"D:\\devtoosl\\git\\local\\tity-sql\\tity-sql\\pom.xml",
			"D:\\devtoosl\\git\\local\\tity-spring-support\\spring-support\\pom.xml",
			"D:\\devtoosl\\git\\local\\tity-test\\tity-test-suite\\pom.xml",
			"D:\\devtoosl\\git\\local\\tity-module-system\\tity-module-system\\system-service\\pom.xml"
	};
	
	public static void main(String[] args) {
		MavenBuilder mbuilder = new MavenBuilder();
		mbuilder.changeVersion("0.7.43.RELEASE");
		mbuilder.install("base-pom.xml");
		mbuilder.deploy("pom.xml");
	}

	
	
	public void changeVersion(String newVersion) {
	
		for (String pom : POMS) {
			changeVersion(pom,newVersion);
		}

	}
	
	private void changeVersion(String pom,String newVersion) {
		String xmls=null;
		try {
			xmls = FileUtil.readString(pom);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String[]  lns=xmls.split("\n");
		for (int i = 0; i < lns.length; i++) {
			int s=lns[i].indexOf("<");
			if(s<=0) continue;
			String prefix=lns[i].substring(0,s);
			String ln=lns[i].substring(s);
			if( ln.startsWith("<modules>") ||  ln.startsWith("<dependencies>") || ln.startsWith("<dependencyManagement>")) break;
			if(ln.startsWith("<version>")) {
				lns[i]=prefix+"<version>"+newVersion+"</version>";
			}
			if(ln.startsWith("<tity.version>")) {
				lns[i]=prefix+"<tity.version>"+newVersion+"</tity.version>";
			}
		}
		
		xmls=StringUtil.join(lns,"\n");
		
		try {
			FileUtil.writeString(pom, xmls);
		} catch (IOException e) {
			e.printStackTrace();
		}
		 
	}
	

	public MavenBuilder() {}

	public void install(String pomFile) {
		String mvn = StringUtil.joinPath(MAVEN_HOME, "/bin/mvn.cmd");
		String cmd = mvn + " clean install -f " + pomFile + " --settings " + SETTINGS_FILE;
		exec(cmd);
	}
	
	public void deploy(String pomFile) {
		String mvn = StringUtil.joinPath(MAVEN_HOME, "/bin/mvn.cmd");
		String cmd = mvn + " deploy -e -f " + pomFile + " --settings " + SETTINGS_FILE;
		exec(cmd);
	}

	/**
	 * 执行本机命令行
	 * 
	 * @param command 命令
	 * @return 命令输出
	 */
	public void exec(String command) {

		BufferedReader br = null;
		try {
			System.err.println(command);
			Process p = Runtime.getRuntime().exec(command, null, WORK_DIR);
			br = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.forName("GBK")));
			String line = null;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

	}

}
